package com.xinfu.newproject.model.certifition;

import android.util.Log;

import com.google.gson.Gson;
import com.xinfu.newproject.apiservice.JsNetworkService;
import com.xinfu.newproject.bean.certifition.UntyingBean;
import com.xinfu.newproject.bean.certifition.UntyingBody;
import com.xinfu.newproject.callback.MyCallBack;
import com.xinfu.newproject.model.IModel;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;

import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.annotations.NonNull;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;

public class UntyingModel implements IModel.IUntyingModel {

    @Override
    public void untying(UntyingBody untyingBody, MyCallBack<UntyingBean> callBack) {
        Map<String, Object> map = new HashMap<>();
        map.put("id", untyingBody.getId());

        Log.i("testAddRoom", new Gson().toJson(map));
        JsNetworkService.getInstance().getIJsNetworkService()
                .untying(untyingBody.getHeader(), untyingBody.getId())
                .throttleFirst(1, TimeUnit.SECONDS)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<UntyingBean>() {
                    @Override
                    public void onSubscribe(@NonNull Disposable d) {
                        Disposable mDisposable; //用于Activity销毁时停止执行
                        mDisposable = d;
                        callBack.onDispose(mDisposable);
                    }

                    @Override
                    public void onNext(@NonNull UntyingBean untyingBean) {

                        callBack.onSuccess(untyingBean);
                    }

                    @Override
                    public void onError(@NonNull Throwable e) {
                        callBack.onError("Server Error", "网络异常，请稍后再试");
                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }
}
